#include <list>
#include <iostream>
#include "command_manager.h"

struct CommandList : public IArgs
{
public:
	std::list<std::string>        Commands;

public:
	CommandList() {}
	~CommandList() {}
};

struct CommandQuit : public IArgs
{
public:
	bool*         				Quit;

public:
	CommandQuit() {}
	~CommandQuit() {}
};

bool On_CommandHelp(CommandManager::Element* _e, IArgs*)
{
	CommandList* CmdList = dynamic_cast<CommandList*>(_e->Datas);
	if (CmdList == NULL)
		return (false);
	std::cout << "Command list:" << std::endl;
	std::list<std::string>::iterator it = CmdList->Commands.begin();
	std::list<std::string>::iterator it_end = CmdList->Commands.end();
	for (; it != it_end; ++it)
	{
		std::cout << (*it) << std::endl;
	}
	std::cout << std::endl;
	return (true);
}

bool On_CommandCoucou(CommandManager::Element* _sender, IArgs*)
{
	return (true);
}

bool On_CommandQuit(CommandManager::Element* _sender, IArgs*)
{
	CommandQuit* _args = dynamic_cast<CommandQuit*>(_sender->Datas);
	if (_args == NULL)
		return (false);
	*(_args->Quit) = true;
	return(true);
}

static CommandManager* CreateCmdMngr(bool* _quit)
{
	CommandManager* CmdMngr = new CommandManager();

	Command* coucou = new Command("coucou");
	Command* quit = new Command("quit");
	Command* help = new Command("help");
	CommandList* Cmds = new CommandList();
	CommandQuit* quitArgs = new CommandQuit();
	quitArgs->Quit = _quit;

	CmdMngr->Add("command_coucou", coucou, On_CommandCoucou, NULL);
	CmdMngr->Add("command_quit", quit, On_CommandQuit, quitArgs);
	CmdMngr->Add("command_help", help, On_CommandHelp, Cmds);

	std::list<Command*> list;
	CmdMngr->GetElements(list);
	std::list<Command*>::iterator it = list.begin();
	for (; it != list.end(); ++it)
	{
		Cmds->Commands.push_back((*it)->GetName());
	}
	return (CmdMngr);
}

int main(int _argc, char** _argv)
{
	bool quit = false;
	CommandManager* CmdMngr = CreateCmdMngr(&quit);

	std::cout << "Welcome!" << std::endl;
	std::cout << "Type help if you need help." << std::endl;
	while (!quit)
	{
		std::string line = "";
		std::string command = "";
		std::list<std::string> params;

		std::getline(std::cin, command);

		CmdMngrArgs args(command);
		CmdMngr->Manage(&args);
	}
	return (EXIT_SUCCESS);
}
